package SGEngine

import (
	"container/list"
	//"fmt"
)

//////////////////////
//扩展 golang 数据集合
//////////////////////

/////动态数组/////////
type ACArrayList struct {
	ArrData []interface{}
	size    int
}

func New_ArrayList(size int) ACArrayList {
	RArr := new(ACArrayList)
	RArr.ArrData = make([]interface{}, size)
	RArr.size = 0
	return *RArr
}

func (a *ACArrayList) Add(data interface{}) {
	if a.size <= cap(a.ArrData)-1 {
		a.ArrData[a.size] = data
		a.size++
	} else {
		NewSize := 0
		if cap(a.ArrData) == 0 {
			NewSize = 2
		} else {
			NewSize = cap(a.ArrData) * 2
		}
		NewArr := make([]interface{}, NewSize)
		copy(NewArr, a.ArrData)
		a.ArrData = NewArr
		a.ArrData[a.size] = data
		a.size++
	}
}

func (a *ACArrayList) RemoveByIndex(Index int) {
	if Index > a.size {
		return
	}

	numMoved := a.size - Index - 1
	if numMoved > 0 {
		copy(a.ArrData[Index:a.size-1], a.ArrData[Index+1:a.size])
	}
	a.ArrData[a.size-1] = nil
	a.size--
	a.CheckReSize()

}

func (a *ACArrayList) CheckReSize() {
	if a.size*2 <= cap(a.ArrData) {
		NewData := make([]interface{}, a.size)
		copy(NewData, a.ArrData[0:a.size])
		a.ArrData = NewData
	}
}

func (a *ACArrayList) Remove(o interface{}) {
	for i := 0; i < a.size; i++ {
		if a.ArrData[i] == o {
			a.RemoveByIndex(i)
		}
	}
}

func (a *ACArrayList) I(Index int) interface{} {
	return a.ArrData[Index]
}

func (a *ACArrayList) GetSize() int {

	return a.size
}

///////////队列/////////////////////////////////
type Queue struct {
	datalist *list.List
}

func NewQueue() *Queue {
	q := new(Queue)
	q.datalist = list.New()
	return q
}
func (q *Queue) Push(Data interface{}) {
	q.datalist.PushBack(Data)
}

func (q *Queue) Pop() {
	q.datalist.Remove(q.datalist.Front())
}

func (q *Queue) Size() int {
	return q.datalist.Len()
}

func (q *Queue) Front() *list.Element {
	return q.datalist.Front()
}

func (q *Queue) Back() *list.Element {
	return q.datalist.Back()
}

func (q *Queue) Each(F func(*list.Element)) {
	for e := q.datalist.Front(); e != nil; e = e.Next() {
		if F != nil {
			F(e)
		}
	}
}
func (q *Queue) Clear() {
	q.datalist = nil
	q.datalist = list.New()
}
